<template>
    <div class="comment-container">
        <h3>发表评论</h3>
        <hr>
        <textarea v-model="content" placeholder="请输入要BB的内容（最多吐槽120字）" maxlength="120"></textarea>
        <mt-button class="sendbtn" type="primary" size="large" @click="postComments">发表评论</mt-button>
        <div class="content">
            <div class="content-cell" v-for="(item,i) in comments" :key="i">
                <p class="userinfo">
                    <span>第{{ i + 1 }}楼</span>
                    <span>用户:{{ item.user_name }}</span>
                    <span>发表时间:{{ item.add_time | dtFormat}}</span>
                </p>
                <p class="usercontent">
                    {{ item.content == 'undefined' ||  !item.content ? '此用户很懒，没说啥' : item.content}}
                </p>
            </div>
            <mt-button type="danger" plain size="large" @click="showMore">加载更多</mt-button>
        </div>
    </div>
</template>

<script>
import {  Toast  } from "mint-ui";
export default {
  data() {
    return {
      comments: [],
      pageindex: 1,
      content : ''
    };
  },
  props: ["id"],
  created() {
    this.getcomments();
  },
  methods: {
    async getcomments() {
      const { data: res } = await this.$http.get(
        "/api/getcomments/" + this.id + "?pageindex=" + this.pageindex
      );
      if (res.status != 0) return Toast("获取评论列表失败!");
      //   this.comments = this.comments.concat(res.message);
      //   this.comments.push.apply(this.comments, res.message);
      this.comments = [...this.comments, ...res.message];
    },
    showMore() {
      this.pageindex++;
      this.getcomments();
    },
    async postComments(){
        if(!this.content.trim()) return Toast('评论内容不能为空');
        const {data:res} = await this.$http.post('/api/postcomment/'+ this.id,{content:this.content})
        if (res.status !=0) return Toast('发布评论失败');
        this.comments.unshift({add_time:Date.now(),content:this.content,user_name:'匿名用户'});
        this.content = ''
    }
  }
};
</script>

<style lang="scss" scoped>
.comment-container {
  textarea {
    margin-bottom: 5px;
    font-size: 14px;
  }
  .content {
    margin-top: 10px;
    .userinfo {
      line-height: 35px;
      background-color: #ccc;
      color: #000;
      margin: 0;
      font-size: 12px;
    }
    .usercontent {
      margin: 5px;
      text-indent: 2em;
    }
  }
}
</style>

